﻿--[[
	Widget name	  	Set
	Author     	 	Kurapica
--]]
     
-- Set
do
	IGAS:NewAddon("IGAS_Studio")
	
    ------------------------------------------------------
	---------------------   Set   -------------------
	------------------------------------------------------
	local _WidgetName = "Set"
    local _Base = "Frame"

    -- Scripts
	_Local = {
		"",
		"-enUS",
		"-zhCN",
		"-zhTW",
		"-frFR",
		"-deDE",
		"-koKR",
		"-ruRU",
		"-esES",
		"-esMX",
	}
	
	local function listInit(self)
		local prefix = self.Parent.Prefix
		
		self:Clear()
				
		for _, i in pairs(_Local) do
			self:AddItem(prefix..i, prefix..i)
		end
				
		self.Value = prefix
	end
	
	local function OnClick_Remove(self)
		local frmSet = self.Parent
		local iCnt = self.ID
		
		while frmSet:GetChild("List_"..iCnt + 1) and frmSet:GetChild("List_"..iCnt + 1).Visible do
			frmSet:GetChild("List_"..iCnt).Value = frmSet:GetChild("List_"..iCnt + 1).Value
			frmSet:GetChild("Txt_"..iCnt).Text = frmSet:GetChild("Txt_"..iCnt + 1).Text
			
			iCnt = iCnt + 1
		end
		
		frmSet:GetChild("Minus_"..iCnt).Visible = false
		frmSet:GetChild("List_"..iCnt).Visible = false
		frmSet:GetChild("Txt_"..iCnt).Visible = false
				
		frmSet.Height = frmSet.Height - (frmSet:GetChild("List_"..iCnt).Height + 10)
		frmSet.Parent.Height = frmSet.Parent.Height - (frmSet:GetChild("List_"..iCnt).Height + 10)
		
		frmSet.__MaxIndex = frmSet.__MaxIndex - 1
	end

	local function OnClick_AddSet(self)
		local iCnt = 1
		local frmSet = self.Parent
		
		while frmSet:GetChild("List_"..iCnt) and frmSet:GetChild("List_"..iCnt).Visible do
			iCnt = iCnt + 1
		end
		
		if frmSet:GetChild("List_"..iCnt) then
			frmSet:GetChild("Minus_"..iCnt).Visible = true
			frmSet:GetChild("List_"..iCnt).Visible = true
			frmSet:GetChild("List_"..iCnt).Value = self.Parent.Prefix
			frmSet:GetChild("Txt_"..iCnt).Visible = true
			frmSet:GetChild("Txt_"..iCnt).Text = ""
		else
			local btnMinus = CreateFrame("NormalButton", "Minus_"..iCnt, frmSet)
			btnMinus.ID = iCnt
			btnMinus.Style = "MINUS"
			btnMinus.OnClick = OnClick_Remove
			
			local cboList = CreateFrame("ComboBox", "List_"..iCnt, frmSet)
			cboList.ID = iCnt
			btnMinus:SetPoint("TOPLEFT", frmSet, "TOPLEFT", 0, - (cboList.Height + 10) * (iCnt - 1) )
			cboList:SetPoint("LEFT", btnMinus, "RIGHT")
			cboList.Width = 150
			cboList.Editable = false
			listInit(cboList)
			
			local txtSet = CreateFrame("SingleTextBox", "Txt_"..iCnt, frmSet)
			txtSet.ID = iCnt
			txtSet:SetPoint("LEFT", cboList, "RIGHT")
			txtSet.Width = 200
			txtSet.Style = "LIGHT"
		end
				
		frmSet.Height = frmSet.Height + frmSet:GetChild("List_"..iCnt).Height + 10
		frmSet.Parent.Height = frmSet.Parent.Height + frmSet:GetChild("List_"..iCnt).Height + 10
		
		frmSet.__MaxIndex = iCnt
	end
		
	-- ScriptType
	local _ScriptType = {
	}
	    
	-- Function
	local _Function = {
		["AddSet"] = function(self, key, value)
			local blnChk = false
			
			if not key or type(key) ~= "string" or string.match(key, "%w+") ~= self.Prefix then
				return
			end
			
			for _, i in pairs(_Local) do
				if self.Prefix..i == key then
					blnChk = true
					break
				end
			end
			
			if blnChk then			
				self.BtnAddSet:Fire("OnClick")
				
				self:GetChild("List_"..self.Count).Value = key
				self:GetChild("Txt_"..self.Count).Text = tostring(value)
			end
		end,
		["Clear"] = function(self)
			for i = self.Count, 1, -1 do
				self:GetChild("Minus_"..i):Fire("OnClick")
			end
		end,
		["GetSet"] = function(self)
			local i = 0
			local iter = function ()
				i = i + 1
				if i > self.Count then
					return nil
				else
					return self:GetChild("List_"..i).Value, self:GetChild("Txt_"..i).Text
				end
			end
			return iter
		end,
		["Load"] = function(self, info)
			self:Clear()
			if not info or type(info) ~= "table" then
				return
			end
			for _, v in ipairs(_Local) do
				if info[self.Prefix..v] then
					self:AddSet(self.Prefix..v, info[self.Prefix..v])
				end
			end
			if self.Count == 0 then
				self:AddSet(self.Prefix, "")
			end
		end,
		["Save"] = function(self, info)
			for _, v in ipairs(_Local) do
				info[self.Prefix..v] = nil
			end
			for iCnt = 1, self.Count do
				if self:GetChild("List_"..iCnt).Value and strtrim(self:GetChild("Txt_"..iCnt).Text) ~= "" then
					info[self:GetChild("List_"..iCnt).Value] = strtrim(self:GetChild("Txt_"..iCnt).Text)
				end
			end
		end,
    }
    
	-- Property
	local _Property = {
		["Prefix"] = {
			["Set"] = function(self, prefix)
				if prefix and type(prefix) == "string" then
					self.__Prefix = prefix
				end
			end,
			
			["Get"] = function(self)
				return self.__Prefix or ""
			end,
			
			["Type"] = IGAS:GetPropertyType("String"),
		},
		["Count"] = {
			["Get"] = function(self)
				return self.__MaxIndex or 0
			end,
			
			["Type"] = IGAS:GetPropertyType("Number"),
		},
    }
    
    -- New    
    local function _New(name, parent)
	
		local frame = CreateFrame("Frame",name,parent)
		
		frame:SetPoint("RIGHT", parent, "RIGHT", -10, 0)
		frame.Height = 24
		
		local btnAdd = CreateFrame("NormalButton", "BtnAddSet", frame)
		btnAdd:SetPoint("BOTTOMLEFT", frame, "BOTTOMLEFT", 0, 0)
		btnAdd.Style = "PLUS"
		btnAdd.OnClick = OnClick_AddSet
	
        return frame
    end
	
    local _WidgetInfo = {
        ["WidgetName"] = _WidgetName,
        ["Base"] = _Base,
        ["Function"] = _Function,
        ["Property"] = _Property,
        ["ScriptType"] = _ScriptType,
        ["New"] = _New,
		-- Special Item
    }
    
    Widget.Set = IGAS:NewWidget(_WidgetInfo)
end